//
//  ReportAlert.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/25.
//

#import "ReportAlert.h"
#import "CommonUtil.h"
#import "ReportTableViewCell.h"


@interface ReportAlert()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UILabel * titleLabel;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) NSArray * datas;
@property (strong, nonatomic) NSMutableArray * selectDatas;
@property (strong, nonatomic) UIButton * cancelBtn;
@property (strong, nonatomic) UIButton * confirmBtn;
@end

@implementation ReportAlert

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.whiteView];
        self.whiteView.layer.cornerRadius = AD(10);
        self.selectDatas = [NSMutableArray array];
        self.datas = @[@"网投,卖料等广告信息",@"发布联系方式",@"攻击侮辱性语言",@"发布淫秽信息",@"其他原因"];
       
    }
    return self;
}

-(void)show{
    self.whiteView.frame = CGRectMake(AD(40), 0, SCREEN_WIDTH-AD(80), AD(370));
    self.whiteView.centerY = SCREEN_HEIGHT/2;
    [self.whiteView addSubview:self.titleLabel];
    [self.whiteView addSubview:self.tableView];
    [self.whiteView addSubview:self.cancelBtn];
    [self.whiteView addSubview:self.confirmBtn];
    [self.tableView reloadData];
}


- (void)cancelBtnClick:(UIButton *)sender{
    [self dismiss];
}

- (void)confirmBtnClick:(UIButton *)sender{
    NSLog(@"确定");
}


#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell refreshWithTitle:self.datas[indexPath.row] selectTitles:self.selectDatas];
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *title = self.datas[indexPath.row];
    if ([self.selectDatas containsObject:title]) {
        for (int i=0; i<self.selectDatas.count; i++) {
            NSString *str = self.selectDatas[i];
            if ([str isEqualToString:title]) {
                [self.selectDatas removeObjectAtIndex:i];
                i--;
            }
        }
        
    }else{
        [self.selectDatas addObject:title];
    }
    [tableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return AD(40);
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _titleLabel.bottom, self.whiteView.width, AD(200))];
        [_tableView registerClass:[ReportTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}



    
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.whiteView.width, AD(80))];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:AD(18)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"举报";
    }
    return _titleLabel;
}

    
-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:AD(18)];
        _cancelBtn.layer.borderWidth = AD(1);
        _cancelBtn.layer.cornerRadius = AD(5);
        CGFloat w = (self.whiteView.width - AD(20) - AD(15) - AD(20))/2;
        _cancelBtn.frame = CGRectMake(AD(20), _tableView.bottom + AD(30), w, AD(35));
    }
    
    return _cancelBtn;
}

-(UIButton *)confirmBtn{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:AD(18)];
        _confirmBtn.layer.cornerRadius = AD(5);
        _confirmBtn.backgroundColor = RGB(255.0, 90.0, 117.0);
        _confirmBtn.frame = CGRectMake(_cancelBtn.right + AD(15), _cancelBtn.top, _cancelBtn.width, _cancelBtn.height);
    }
    
    return _confirmBtn;
}
@end
